package com.devbobcorn.nekorization.client.rendering;

import com.devbobcorn.nekorization.client.model.SpriteModel;
import com.devbobcorn.nekorization.entities.ModEntityType;
import com.devbobcorn.nekorization.entities.SpriteEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;

import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;

public class SpriteOnShoulderRenderer {
    private final SpriteModel<SpriteEntity> model;

    public SpriteOnShoulderRenderer(ModelPart modelPart){
        this.model = new SpriteModel<SpriteEntity>(modelPart);
    }

    public void render(PoseStack stack, MultiBufferSource buffers, int l, Player player, float r, float g, float b, float a) {
        stack.mulPose(Vector3f.XP.rotationDegrees(180.0F));
		stack.mulPose(Vector3f.YP.rotationDegrees(player.yBodyRot));
        this.render(stack, buffers, l, player, r, g, b, a, true);
        this.render(stack, buffers, l, player, r, g, b, a, false);
    }

    private void render(PoseStack stack, MultiBufferSource buffers, int l, Player player, float r, float g, float b, float a, boolean left) {
        CompoundTag compoundtag = left ? player.getShoulderEntityLeft() : player.getShoulderEntityRight();
        EntityType.byString(compoundtag.getString("id")).filter((type) -> {
            return type == ModEntityType.$SPRITE_TYPE.get();
        }).ifPresent((type) -> {
            stack.pushPose();
            stack.translate(left ? (double) 0.4F : (double) -0.4F, player.isCrouching() ? -2.7 : -2.9, 0.0D);
            VertexConsumer vertexconsumer = buffers.getBuffer(this.model.renderType(SpriteRenderer.GRAY));
            this.model.renderToBuffer(stack, vertexconsumer, l, OverlayTexture.NO_OVERLAY, r, g, b, a);
            stack.popPose();
        });
    }
}
